package com.zhou.common.pub.exception;

import com.zhou.common.pub.enums.BaseEnum;
import com.zhou.common.pub.util.MessageUtils;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

public class BizException extends RuntimeException {

    @Getter
    private String code;

    private String msg;

    private Object[] args = new Object[]{};

    public BizException(BaseEnum baseEnum) {
        super(baseEnum.getMsg().toString());
        this.code = baseEnum.getCode().toString();
        this.msg = baseEnum.getMsg().toString();
    }

    public BizException(Integer code, String msg) {
        super(msg);
        this.code = code + "";
        this.msg = msg;
    }

    public BizException(String code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public BizException(String code) {
        super(code);
        this.code = code;
    }

    public BizException(String code, Object... args) {
        super(code);
        this.code = code;
        this.args = args;
    }

    public BizException(Integer code, String msg, Object... args) {
        super(msg);
        this.code = code + "";
        this.msg = msg;
        this.args = args;
    }

    public BizException(String code, String msg, Object... args) {
        super(msg);
        this.code = code + "";
        this.msg = msg;
        this.args = args;
    }

    public String getMsg() {
        String message = msg;
        if (StringUtils.isNotEmpty(code)) {
            if (StringUtils.isEmpty(msg)) {
                message = MessageUtils.message(code, args);
            } else {
                message = MessageUtils.message(code, msg, args);
            }
        }
        return message;
    }

}
